const Base = require('./base.js');

module.exports = class extends Base {
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const list = await this.model('role').page(page, size).countSelect();

    return this.success(list);
  }
  async listAllAction() {
    const list = await this.model('role').where({status: 0}).select();

    return this.success(list);
  }
  async addAction() {
    const ids = this.post('roles');
    const roleId = this.post('roleId');

    const list = await this.model('role').where({id: roleId}).update({
      roles: ids
    });
    return this.success(list);
  }
  async putAction() {
    const value = this.post();
    await this.model('role').add({
      roleCode: value.roleCode,
      roleDesc: value.roleDesc,
      roleName: value.roleName,
      create_time: ['exp', 'CURRENT_TIMESTAMP()']
    });
    return this.success();
  }
  async treeAction() {
    const id = this.get('id');
    const list = await this.model('role').where({id: id}).find();
    return this.success(list);
  }

  async delAction() {
    const id = this.get('id');
    const model = this.model('role');
    if (id === '5') {
      return this.fail(400, '这个不能删！');
    }
    await model.where({id: id}).limit(1).delete();
    return this.success();
  }

  async addRuleAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('set');

    values.label = values.name;

    await model.where({id: id}).update({
      content: values.content
    });
    return this.success();
  }
};
